from .champion import Mage
from .item import Item
from typing import Dict, Any, Optional


class XiaoQiao(Mage):
    """小乔 - 入场召唤3个周瑜"""
    
    def __init__(self):
        super().__init__(
            card_id="xiao_qiao",
            name="Xiao Qiao",
            image_path="Xiao Qiao.jpg",
            description="Mage: Does not take counter damage when attacking.\nOn summon: Summon 3 Zhou Yu",
            cost=5,
            attack=1,
            hp=3
        )
    
    def on_summon(self, game_state, player_id: str):
        """入场时召唤3个周瑜"""
        battlefield = game_state.battlefields[player_id]
        summoned_count = 0
        
        # 获取XiaoQiao的instance_id
        xiao_qiao_id = None
        for champion in battlefield:
            if champion and hasattr(champion, 'card_id') and champion.card_id == "xiao_qiao":
                xiao_qiao_id = champion.instance_id
                break
        
        for i, slot in enumerate(battlefield):
            if slot is None and summoned_count < 3:
                # 创建周瑜实例
                from .guard import ZhouYu
                zhou_yu = ZhouYu()
                import uuid
                zhou_yu.instance_id = f"{player_id}_{i}_zhou_yu_{uuid.uuid4().hex[:8]}"
                zhou_yu.position = i
                # 设置守护目标为XiaoQiao
                if xiao_qiao_id:
                    zhou_yu.guarding_target_id = xiao_qiao_id
                battlefield[i] = zhou_yu
                summoned_count += 1


class MiYue(Mage):
    """芈月 - PP上限≥6时对敌方英雄造成伤害后令其立即死亡"""
    
    def __init__(self):
        super().__init__(
            card_id="mi_yue",
            name="Mi Yue",
            image_path="Mi Yue.jpg",
            description="Mage: Does not take counter damage when attacking.\nIf max PP ≥ 6: Enemy heroes die when damaged by this unit",
            cost=2,
            attack=2,
            hp=1
        )
    
    def on_deal_damage(self, game_state, target, damage: int, player_id: str):
        """造成伤害后检查是否触发即死效果"""
        # 检查PP上限是否≥6
        if game_state.max_pp[player_id] >= 6:
            # 检查目标是否为敌方英雄(非召唤师)
            if hasattr(target, 'card_id') and not target.card_id.endswith('_summoner'):
                # 令目标立即死亡
                target.hp = 0
                # 从战场移除死亡的单位
                if hasattr(target, 'instance_id'):
                    game_state._remove_dead_champion(target.instance_id)
